package com.sbtr.business.classify.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbtr.business.calibration.service.ICalibrationService;
import com.sbtr.business.calibration.service.impl.CalibrationDetailsServiceImpl;
import com.sbtr.business.classify.dto.AlgorithmInfoDto;
import com.sbtr.business.classify.entity.Classify;
import com.sbtr.business.classify.mapper.ClassifyMapper;
import com.sbtr.business.classify.service.IClassifyService;
import com.sbtr.open.calibration.dto.CalibrationDetailsDto;
import com.sbtr.open.calibration.dto.CalibrationDto;
import com.sbtr.open.classify.dto.ClassifyCountDto;
import com.sbtr.open.classify.dto.ClassifyDto;
import com.sbtr.util.BeanCopyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author oujh5

 */
@Transactional
@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify>
		implements IClassifyService {

	private static final Logger logger = LoggerFactory.getLogger(ClassifyServiceImpl.class);



	@Autowired
	private AlgorithmInfoServiceImpl algorithmInfoServiceImpl;


//	@Autowired
//	private NlpAlgorithmServiceImpl nlpAlgorithmServiceImpl;

	@Autowired
	private CalibrationDetailsServiceImpl calibrationDetailsService;


	@Autowired
	private ICalibrationService calibrationService;

	@Autowired
	private ClassifyMapper calibrationmapper;


	@Override
	public List<ClassifyDto> search(Long calibrationId) {
		logger.info("根据标定集id查询：{}", calibrationId);
		QueryWrapper<Classify> wrapper = new QueryWrapper<>();
		wrapper.eq("calibration_id", calibrationId);
		return BeanCopyUtils.copyProperties(list(wrapper), ClassifyDto.class);
	}

	@Override
	public List<ClassifyDto> searchById(Long calibrationDetailsId){
		CalibrationDetailsDto calibrationDetailsDto = calibrationDetailsService.findById(calibrationDetailsId);
		return search(calibrationDetailsDto.getCalibrationId());
	}


	@Override
	public ClassifyDto saveClassify(ClassifyDto dto) {
		Assert.isTrue(null != dto, "标签信息不能为空");
		Assert.notNull(dto.getCalibrationId(), "标定集id不能为空");
		Classify cla = BeanCopyUtils.copyProperties(dto, Classify.class);
		QueryWrapper<Classify> wrapper = new QueryWrapper<>();
		wrapper.eq("classify_name", cla.getClassifyName());
		wrapper.eq("calibration_id", cla.getCalibrationId());
		Assert.isTrue(this.count(wrapper) == 0, "标签名称已经存在");
		QueryWrapper<Classify> wrapper2 = new QueryWrapper<>();
		wrapper2.eq("shortcut", cla.getShortcut());
		wrapper2.eq("calibration_id", cla.getCalibrationId());
		Assert.isTrue(this.count(wrapper2) == 0, "标签快捷键已经存在");

		save(cla);
//		dto.setId(cla.getId());

		//查询标签的数量
		ClassifyCountDto count=calibrationmapper.classifyCount(cla.getCalibrationId());

         //更新我的主任务和我被分配的任务的标签数
		List<CalibrationDetailsDto> calibrationDetailsDto=calibrationDetailsService.searchDetailsCalibrationId(cla.getCalibrationId());

		CalibrationDto calibration=calibrationService.findById(cla.getCalibrationId());
		calibration.setClassifyCount(count.getClassifyCount());
		calibrationService.update(calibration);

		for(CalibrationDetailsDto detailsDto:calibrationDetailsDto){
			detailsDto.setClassifyCount(count.getClassifyCount());
			calibrationDetailsService.update(detailsDto);
		}

		return dto;
	}



	@Override
	public ClassifyDto updateClassify(ClassifyDto dto) {
		Assert.isTrue(null != dto, "标签信息不能为空");
		Classify cla = BeanCopyUtils.copyProperties(dto, Classify.class);
		QueryWrapper<Classify> wrapper = new QueryWrapper<>();
		wrapper.eq("classify_name", cla.getClassifyName());
		wrapper.eq("calibration_id", cla.getCalibrationId());
		wrapper.ne("id", cla.getId());
		Assert.isTrue(this.count(wrapper) == 0, "标签名称已经存在");
		QueryWrapper<Classify> wrapper2 = new QueryWrapper<>();
		wrapper2.eq("shortcut", cla.getShortcut());
		wrapper2.eq("calibration_id", cla.getCalibrationId());
		wrapper2.ne("id", cla.getId());
		Assert.isTrue(this.count(wrapper2) == 0, "标签快捷键已经存在");
		this.updateById(BeanCopyUtils.copyProperties(dto, Classify.class));
		return BeanCopyUtils.copyProperties(dto, ClassifyDto.class);
	}

	@Override
	public boolean batchSave(Long calibrationId,List<ClassifyDto> dtos){
		Assert.isTrue(null != dtos, "标签信息不能为空");
		Assert.notNull(calibrationId, "标定集id不能为空");
		for(ClassifyDto cls:dtos){
			cls.setCalibrationId(calibrationId);
			QueryWrapper<Classify> wrapper = new QueryWrapper<>();
			wrapper.eq("classify_name", cls.getClassifyName());
			wrapper.eq("calibration_id", cls.getCalibrationId());
			Assert.isTrue(this.count(wrapper) == 0, "标签名称已经存在");
			QueryWrapper<Classify> wrapper2 = new QueryWrapper<>();
			wrapper2.eq("shortcut", cls.getShortcut()); // 快捷键，前端硬编码，暂时废弃
			wrapper2.eq("calibration_id", cls.getCalibrationId());
			Assert.isTrue(this.count(wrapper2) == 0, "标签快捷键已经存在");
		}
		List<Classify> details = BeanCopyUtils.copyProperties(dtos, Classify.class);
		boolean tag = saveOrUpdateBatch(details); //  批量更新保存

		//查询标签的数量
		ClassifyCountDto count=calibrationmapper.classifyCount(calibrationId);

		//更新我的主任务和我被分配的任务的标签数
		List<CalibrationDetailsDto> calibrationDetailsDto=calibrationDetailsService.searchDetailsCalibrationId(calibrationId);

		CalibrationDto calibration=calibrationService.findById(calibrationId);
		calibration.setClassifyCount(count.getClassifyCount());
		calibrationService.update(calibration);

		for(CalibrationDetailsDto detailsDto:calibrationDetailsDto){
			detailsDto.setClassifyCount(count.getClassifyCount());
			calibrationDetailsService.update(detailsDto);
		}

		return tag;
	}

	@Override
	public boolean deleteClassify(Long id) {
		Assert.isTrue(null != id, "标签id不能为空");
		Classify byId = getById(id);
		Assert.isTrue(null != byId, "标签不存在");
		boolean tag =  removeById(id);
		//查询标签的数量
		ClassifyCountDto count=calibrationmapper.classifyCount(byId.getCalibrationId());

		//更新我的主任务和我被分配的任务的标签数
		List<CalibrationDetailsDto> calibrationDetailsDto=calibrationDetailsService.searchDetailsCalibrationId(byId.getCalibrationId());

		CalibrationDto calibration=calibrationService.findById(byId.getCalibrationId());
		calibration.setClassifyCount(count.getClassifyCount());
		calibrationService.update(calibration);

		for(CalibrationDetailsDto detailsDto:calibrationDetailsDto){
			detailsDto.setClassifyCount(count.getClassifyCount());
			calibrationDetailsService.update(detailsDto);
		}
		return tag;
	}













//	@Override
//	@GetMapping(value = "/updateCountNum")
//	public boolean updateCountNum(@RequestParam("id") Long id, @RequestParam("num") int num) {
//		baseMapper.updateCountNum(id, num);
//		return true;
//	}

//	@Override
//	@PostMapping(value = "/updateBatchCountNum")
//	public boolean updateBatchCountNum(@RequestBody List<Item> itemList) {
//		baseMapper.updateBatchCountNum(itemList);
//		return true;
//	}

	@Override
	public List<AlgorithmInfoDto> getaAlgorithmInfo() {
		return algorithmInfoServiceImpl.getAlgorithmInfo();
	}


	@Override
	public List<AlgorithmInfoDto> getAlgorithmInfoByProjectType(String projectType){
		return algorithmInfoServiceImpl.getAlgorithmInfoByProjectType(projectType);
	}


//	@Override
//	@GetMapping(value = "/get-nlp-algorithm")
//	public List<NlpAlgorithmDto> getNlpAlgorithm() {
//		return nlpAlgorithmServiceImpl.getNlpAlgorithm();
//	}

	@Override
	public int selectMaxSn() {
		return baseMapper.selectMaxSn();
	}

}
